var category_type = null;
var progect_type = null;
var project_hivetype = null;
var project_state = null;
var voltage_type = null;
var bprojectapplicationall = null;//申请书
var bbudgetstandardall = null;//预算标准
var orgall = null;//所有人员
var userall = null;//所有单位
var userallmy = null;
var map = null;
$(function () {
    $.when(getdatadictionary("category_type"),
		  getdatadictionary("progect_type"),
		  getdatadictionary("project_hivetype"),
          getdatadictionary("project_state"),
          getdatadictionary("voltage_type"),
          getbprojectapplicationall(),
          getbbudgetstandardall(),
          getorgall(),
          getuserall()).done(
        $("#jqGrid").jqGrid({
        url: baseURL + 'bss/apbase/list?state=isdelno&ourid=isok',
        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden: true  },
			{ label: '所属批次ID', name: 'bGuid', index: 'b_guid', width: 80 ,hidden: true },
			{ label: '业务类型', name: 'pBCategory', index: 'p_b_category', width: 80,hidden: true,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in category_type) {
                        if (cellvalue == category_type[d].dKey) return category_type[d].dValue;
                    }
                }
			},
			{hidden: true, label: '项目类别', name: 'pBType', index: 'p_b_type', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in progect_type) {
                        if (cellvalue == progect_type[d].dKey) return progect_type[d].dValue;
                    }
                }
			},
			{ hidden: true, label: '储备状态', name: 'pBHive', index: 'p_b_hive', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in project_hivetype) {
                        if (cellvalue == project_hivetype[d].dKey) return project_hivetype[d].dValue;
                    }
                }
			},
			{ label: '项目状态', name: 'pBState', index: 'p_b_state', width: 67,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in project_state) {
                        if (cellvalue == project_state[d].dKey) return project_state[d].dValue;
                    }
                }
			},
			{ label: '项目名称', name: 'pBName', index: 'p_b_name', width: 150 },
			{hidden: true, label: '项目地址', name: 'pBAddress', index: 'p_b_address', width: 80 },
			{ label: '项目经度', name: 'pBLongitude', index: 'p_b_longitude', width: 80 ,hidden: true},
			{ label: '项目纬度', name: 'pBLatitude', index: 'p_b_latitude', width: 80,hidden: true },
			{ label: '项目单位', name: 'pBOrgid', index: 'p_b_orgid', width: 80,
                formatter: function (cellvalue, options, rowObject) {

                    for (d in orgall) {
                        if (cellvalue == orgall[d].deptId)
                            return orgall[d].name;
                    }
                }
            },
			{hidden: true, label: '所属申请书', name: 'aGuid', index: 'a_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {

                    for (d in bprojectapplicationall) {
                        if (cellvalue == bprojectapplicationall[d].guid) return bprojectapplicationall[d].pAName;
                    }
                }
            },
			{hidden: true, label: '所属预算标准', name: 'sGuid', index: 's_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in bbudgetstandardall) {
                        var ss = cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");

                        for ( var i = 0; i <ss.length; i++)
                        {
                            if (ss[i]==bbudgetstandardall[d].guid) {
                                names += bbudgetstandardall[d].bSName + ",";
                            }
                        }


                    }
                    if (names.length > 0) {
                        names = names.substring(0, names.length - 1);
                    }
                    return names;
                }
            },
			{ hidden: true,label: '最高电压等级', name: 'pBVoltage', index: 'p_b_voltage', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in voltage_type) {
                        if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                    }
                }
			},
			{ label: '参与人', name: 'pBCollectid', index: 'p_b_collectid', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";
                    for (d in userall) {
                        var ss = (cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "") + "").split(",");
                        for ( var i = 0; i <ss.length; i++)
                        {
                            if (ss[i]==userall[d].userId+"") {
                                 names += userall[d].username + ",";
                            }
                        }
                    }
                    if (names.length > 0) {
                        names = names.substring(0, names.length - 1);
                    }
                    return names;
                }
            },

            { label: '创建时间', name: 'pBAddtime', index: 'p_b_addtime', width: 80,
                formatter:function(value,options,row){
                 return value!=null?new Date(value).Format('yyyy-MM-dd hh:mm'):"";}
            },
			{ label: '总投资', name: 'priceCount', index: 'priceCount', width: 80 },
		   {
                label: '操作', name: '', width: 300, formatter: function (cellvalue, options, rowObject) {

                //  遵义维持原版所有功能
                return "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='showgs(\"" + rowObject.guid + "\",\"" + rowObject.pBType + "\")' >&nbsp;估算调整</a>"
                    +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportspoint(\"" + rowObject.guid + "\")' >&nbsp;导出点位明细</a>"
                    +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exports(\"" + rowObject.guid + "\")' >&nbsp;导出申请书</a><br>"
                    +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportsimg(\"" + rowObject.guid + "\")' >&nbsp;导出照片</a>"
                    +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportshz(\"" + rowObject.guid + "\")' >&nbsp;导出材料汇总</a>"
                    +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportsmx(\"" + rowObject.guid + "\")' >&nbsp;导出材料明细</a>"

                //  大荔要求注释掉【估算调整】和【导出申请书】
               // return  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportspoint(\"" + rowObject.guid + "\")' >&nbsp;导出点位明细</a>"
               //     +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportshz(\"" + rowObject.guid + "\")' >&nbsp;导出材料汇总</a>"
               //     +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportsmx(\"" + rowObject.guid + "\")' >&nbsp;导出材料明细</a>"
               //     +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportsimg(\"" + rowObject.guid + "\")' >&nbsp;导出照片</a>"
           }
            }

        ],
		viewrecords: true,
        height: 500,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    })
	);

});

function exports(guid)
{

    location.href=baseURL + "bss/apbase/exports?guid="+guid;

}
function exportsimg(guid)
{

    location.href=baseURL + "bss/apbase/exportsimg?guid="+guid;

}
function exportspoint(guid)
{

    location.href=baseURL + "bss/apbase/exportsbypoint?guid="+guid;

}
function exportshz(guid)
{

    location.href=baseURL + "bss/apbase/exportsbyhz?guid="+guid;

}
function exportsmx(guid)
{

    location.href=baseURL + "bss/apbase/exportsbymx?guid="+guid;

}

function showgs(guid,ptype)
{
    var title ="项目估算";
    var content = "apbaseothorgs.html?guid=" + guid+"&ptype="+ptype;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['95%', '95%'],
        content: content

    });
}



function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="category_type")
        {
            category_type=r.returnlist;
        }
        if(sss=="progect_type")
        {
            progect_type=r.returnlist;
        }
        if(sss=="project_hivetype")
        {
            project_hivetype=r.returnlist;
        }
        if(sss=="project_state")
        {
            project_state=r.returnlist;
        }
        if(sss=="voltage_type")
        {
            voltage_type=r.returnlist;
        }
        vm.category_type=category_type;
        vm.progect_type=progect_type;
        vm.project_hivetype=project_hivetype;
        vm.project_state=project_state;
        vm.voltage_type=voltage_type;


    });
}


function getbprojectapplicationall()
{
    $.get(baseURL + "api/bprojectapplication/select", function (r) {
            bprojectapplicationall = r.returnlist;
    });
}
function  getbbudgetstandardall()
{
    $.get(baseURL + "api/bbudgetstandard/select" , function (r) {
        bbudgetstandardall = r.returnlist;
    });
}
function  getorgall()
{
    $.get(baseURL + "sys/dept/selectall" , function (r) {
        orgall = r.returnlist;
    });
}
function  getuserall()
{
    $.get(baseURL + "sys/user/select" , function (r) {
        userall = r.returnlist;
        userallmy = r.myuserlist;
    });
    vm.userall=userall;
    vm.userallmy=userallmy;
}



var vm = new Vue({
	el:'#rrapp',

	data:{
        q:{
            pBType:null,
            pBHive:null,
            pBName:null,
            pBState:null
        },
        bprojectapplicationall:bprojectapplicationall,
        bbudgetstandardall : bbudgetstandardall,
        orgall : orgall,
        userall : userall,
        userallmy : userallmy,

        showList: true,
		title: null,
		aPBase: {pBCollectid: []},
        status: ["是", "否"],

        category_type:category_type,
        progect_type:progect_type,
        project_hivetype:project_hivetype,
        project_state:project_state,
        voltage_type:voltage_type

	},
	methods: {
		query: function () {
			vm.reload();
		},

		add: function(){
			vm.showList = false;
			vm.title = "新增";
            vm. aPBase={pBCollectid: [],pBLongitude:null,pBLatitude:null},
            vm.category_type=category_type;
             vm.progect_type=progect_type;
            vm.project_hivetype=project_hivetype;
             vm.project_state=project_state;
            vm.voltage_type=voltage_type;
             vm.userall= userall;
            vm.userallmy=userallmy;
		},
        getdiwei:function (Longitude,Latitude)
        {
            vm. aPBase.pBLongitude=Longitude;
            vm. aPBase.pBLatitude=Latitude;
        },
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.category_type=category_type;
            vm.progect_type=progect_type;
            vm.project_hivetype=project_hivetype;
            vm.project_state=project_state;
            vm.voltage_type=voltage_type;
            vm.userall= userall;
            vm.getInfo(guid);

		},



		saveOrUpdate: function (event) {
			var url = vm.aPBase.guid == null ? "bss/apbase/save" : "bss/apbase/update";

			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.aPBase),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/apbase/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/apbase/info/"+guid, function(r){
                vm.aPBase = r.aPBase;
                vm.aPBase.pBCollectid = r.aPBase.pBCollectid.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");
                vm.userallmy=r.userallmy;
                var i = 0;
                var item=new Array();
                for (d in progect_type) {

                    if (progect_type[d].dKey ==r.aPBase.pBType) {

                        item[i] = progect_type[d];
                        i++;
                    }
                }


               vm.progect_type=item;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                	'pBType': vm.q.pBType,
                    'pBHive': vm.q.pBHive,
                    'pBName': vm.q.pBName,
                    'pBState': vm.q.pBState

				   },
                page:page
            }).trigger("reloadGrid");
		}
	}
});